<!-- -->
<template>
  <div class="information">
    <el-container>
      <el-main>
        <el-container>
          <el-header style="text-align:left;line-height:60px">
            <span>我的信息</span>
          </el-header>
          <el-main>
            <div class="informationContent">
              <el-form
                :label-position="labelPosition"
                label-width="120px"
                :model="informationList"
              >
                <el-form-item label="昵称">
                  <el-row>
                    <el-col :span="6">
                      <el-input
                        v-model="informationList.nickname"
                        auto-complete="on"
                        :readonly="true"
                      ></el-input>
                    </el-col>
                    <el-col :span="4">性别：</el-col>
                    <el-col :span="6">
                      <el-select
                        v-model="informationList.gender"
                        placeholder="请选择性别"
                        auto-complete="on"
                        :disabled="true"
                      >
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="2"></el-option>
                      </el-select>
                    </el-col>
                  </el-row>
                </el-form-item>
                <el-form-item label="手机号：">
                  <el-row>
                    <el-col :span="15">
                      <el-input
                        v-model="informationList.phone"
                        auto-complete="on"
                        :readonly="true"
                      ></el-input>
                    </el-col>
                  </el-row>
                </el-form-item>

                <el-form-item label="邮箱：">
                  <el-row>
                    <el-col :span="15">
                      <el-input
                        v-model="informationList.email"
                        auto-complete="on"
                        :readonly="true"
                      ></el-input>
                    </el-col>
                  </el-row>
                </el-form-item>
                <el-form-item label="生日：">
                  <el-row>
                    <el-col :span="15">
                      <el-input
                        v-model="informationList.birthday"
                        :readonly="true"
                      ></el-input>
                    </el-col>
                  </el-row>
                </el-form-item>
                <el-form-item label="简介：">
                  <el-row>
                    <el-col :span="15">
                      <el-input
                        type="textarea"
                        v-model="informationList.info"
                        placeholder="请填写用户简介"
                        auto-complete="on"
                        :readonly="true"
                      ></el-input>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-form>
            </div>
          </el-main>
        </el-container>
      </el-main>
      <el-aside width="200px">
        <div class="headImage">
          <img :src="informationList.image" alt />
          <h3>{{ informationList.name }}</h3>
          <el-button @click="fix()">点击修改</el-button>
          <el-dialog
            title="修改信息"
            :visible.sync="dialogVisible"
            width="50%"
            style="text-align:left"
          >
            <el-form
              :label-position="labelPosition"
              label-width="80px"
              :model="fixForm"
            >
              <el-form-item label="昵称">
                <el-row>
                  <el-col :span="6">
                    <el-input
                      v-model="fixForm.nickname"
                      placeholder="请输入名字"
                      auto-complete="on"
                    ></el-input>
                  </el-col>
                  <el-col :span="2">性别：</el-col>
                  <el-col :span="6">
                    <el-select
                      v-model="fixForm.gender"
                      placeholder="请选择性别"
                      auto-complete="on"
                    >
                      <el-option label="男" :value="1"></el-option>
                      <el-option label="女" :value="2"></el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="头像：">
                <el-upload
                  class="avatar-uploader"
                  action="1"
                  :show-file-list="false"
                  :http-request="UploadImage"
                >
                  <img style="height:100px;width:100px;" v-if="imageUrl" :src="imageUrl" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="生日：">
                <el-row>
                  <el-col :span="15" style="text-align:left">
                    <el-date-picker
                      v-model="fixForm.birthday"
                      type="datetime"
                      placeholder="选择日期"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      auto-complete="on"
                    ></el-date-picker>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="手机：">
                <el-input
                  v-model="fixForm.phone"
                  auto-complete="on"
                  placeholder="请填写您的手机号"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱：">
                <el-input
                  v-model="fixForm.email"
                  auto-complete="on"
                  placeholder="请填写您的邮箱"
                ></el-input>
              </el-form-item>
              <el-form-item label="简介：">
                <el-row>
                  <el-col>
                    <el-input
                      type="textarea"
                      v-model="fixForm.info"
                      placeholder="请填写您的简介"
                      auto-complete="on"
                    ></el-input>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="commit()">确 定</el-button>
            </span>
          </el-dialog>
        </div>
      </el-aside>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Information",
  data() {
    return {
      userId: "",
      labelPosition: "right",
      dialogVisible: false,
      imageUrl: "",
      fileList: [],
      // 个人信息列表
      informationList: {
        id: "",
        name: "",
        gender: "",
        phone: "",
        email: "",
        birthday: "",
        info: "",
        image: "",
        nickname: "",
        password: ""
      },
      fixForm: {}
    };
  },
  created() {
    this.getUserInformation();
  },

  components: {},

  filters: {},

  computed: {},

  methods: {
    Success(index) {
      this.$message.success(index);
    },
    Fail(index) {
      this.$message.error(index);
    },
    fix() {
      this.fixForm = this.informationList;
      this.imageUrl = this.informationList.image;
      this.dialogVisible = true;
    },
    commit() {
      if (
        this.fixForm.nickname == "" ||
        this.fixForm.birthday == "" ||
        this.fixForm.email == "" ||
        this.fixForm.phone == "" ||
        this.fixForm.gender == "" ||
        this.fixForm.info == ""
      ) {
        this.Fail("信息不足，请重新输入信息");
      } else {
        this.dialogVisible = false;
        this.axios
          .post("/api/user", {
            id: this.fixForm.id,
            username: this.fixForm.username,
            password: this.fixForm.password,
            email: this.fixForm.email,
            phone: this.fixForm.phone,
            nickname: this.fixForm.nickname,
            gender: this.fixForm.gender,
            birthday: this.fixForm.birthday,
            image: this.imageUrl,
            info: this.fixForm.info
          })
          .then(res => {
            console.log(res.data);
            if (res.data.code === 200) {
              this.Success("修改成功");
              this.getUserInformation();
            } else {
              this.Fail("修改失败");
            }
          })
          .catch(error => {
            console.log(error);
            this.Fail("修改失败");
          });
      }
    },
    UploadImage(param) {
      // console.log(param)
      const formData = new FormData();
      formData.append("file", param.file);
      this.axios
        .post("/api/upload", formData)
        .then(res => {
          // console.log(res);
          if (res.data.code === 200) {
            this.imageUrl = res.data.data;
            console.log(this.imageUrl);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getUserInformation() {
      this.userId = sessionStorage.getItem("userId");
      // this.userId = "13";

      if (this.userId != "") {
        this.axios
          .post("/api/user/findById", {
            id: this.userId
          })
          .then(res => {
            // console.log(res.data);
            if (res.data.code === 200) {
              this.informationList = res.data.data;
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    }
  }
};
</script>
<style scoped>
.information {
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}
.information::-webkit-scrollbar {
  display: none;
}
.el-container {
  width: 100%;
  height: 100%;
  /* background-color: black; */
}

.el-aside {
  /* background-color: #d3dce6; */
  color: #333;
  /* text-align: center; */
  /* line-height: 200px; */
  border-left-style: dashed;
  border-width: 1px;
  border-left-color: #dcdfe6;
}
.el-header {
  width: 100%;
  height: 5%;
  /* background-color: rebeccapurple; */
}

.el-main {
  background-color: white;
  color: #333;
  padding: 0;
  /* text-align: center; */
  /* line-height: 160px; */
}
.informationContent {
  width: 100%;
  height: 100%;
}
.headImage {
  margin-top: 28px;
  width: 100%;
  height: 50%;
}
.headImage img {
  width: 80%;

  border-radius: 50%;
}
</style>